package com.kuanter.kuanterauto.setting;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.OverlayItem;
import com.kuanter.kuanterauto.HomeActivityGroup;
import com.kuanter.kuanterauto.MainActivity;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.dataservices.PushServices;
import com.kuanter.kuanterauto.entity.MapResult;
import com.kuanter.kuanterauto.entity.WebSiteInfo;
import com.kuanter.kuanterauto.entity.WebSiteInfoList;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.userlogin.UserLoginActivity;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.StringUtils;
import com.kuanter.kuanterauto.website.WebSiteActivity;
import com.umeng.analytics.MobclickAgent;

public class SettingActivity extends BaseActivity_NoBar{

	private View rl_setting_login = null;
	private View rl_setting_service = null;
	private View rl_setting_aboutus = null;
	private View rl_setting_feedback = null;
	TextView tv_setting_username = null;
	TextView tv_setting_login = null;
	
	private ToggleButton tb_setting_dajiao = null;
	Boolean loginFlag = false;
	Button bt_setting_quit = null;
	DataLoader dataLoader=null;
	
	MapResult mapr=null;
	
	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case 1:
				break;
			case -1:
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.setting);
		dataLoader=new DataLoader(this);
		mapr=MapResult.getMapResults();
		
		initView();
		initListener();
	}
	
	private void initView(){
		rl_setting_login=(View) findViewById(R.id.rl_setting_login);
		rl_setting_service=(View) findViewById(R.id.rl_setting_service);
		rl_setting_aboutus=(View) findViewById(R.id.rl_setting_aboutus);
		rl_setting_feedback=(View) findViewById(R.id.rl_setting_feedback);
		tb_setting_dajiao=(ToggleButton) findViewById(R.id.tb_setting_dajiao);
		tv_setting_username = (TextView) findViewById(R.id.tv_setting_username);
		tv_setting_login = (TextView) findViewById(R.id.tv_setting_login);
		
		bt_setting_quit = (Button) findViewById(R.id.bt_setting_quit);
		
		SharedPreferences sp = getSharedPreferences(
				"userinfo", MODE_PRIVATE);
		String username=sp.getString("userRealName", "");
		if(username.length()>0)
		{
			tv_setting_username.setText(username);
			tv_setting_login.setText("注销");
			loginFlag = true;
		}
		
		if(sp.getInt("notAlert", 0)==1){
			tb_setting_dajiao.setChecked(true);
		}else{
			tb_setting_dajiao.setChecked(false);
		}
			
		
		
	}
	
	private void initListener(){
		rl_setting_login.setOnClickListener(this);
		rl_setting_service.setOnClickListener(this);
		rl_setting_aboutus.setOnClickListener(this);
		rl_setting_feedback.setOnClickListener(this);
		tb_setting_dajiao.setOnClickListener(this);
		bt_setting_quit.setOnClickListener(this);
		
		
	}
	
	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.rl_setting_login:
			if(MainActivity.ID!=0)
			{
				List<NameValuePair> p = new ArrayList<NameValuePair>();
				SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
				String id=sp.getInt("id", 0)+"";
				
				p.add(new BasicNameValuePair("Id", id));
//				HttpTask task = new HttpTask(mHandler,p,this);
//				task.execute(MainConstants.HOST + "userInfo/end_login");// url
				dataLoader.DisplayData(MainConstants.HOST + "userInfo/end_login", p, mHandler, "",false);
				
				
				SharedPreferences.Editor editor = sp.edit();
				editor.putInt("id", 0);
				editor.putInt("notAlert", 0);
				editor.putString("mobileNumber", "");
				editor.putString("userEmail", "");
				editor.putString("userRealName", "");
				editor.commit();
				
				tv_setting_username.setText("用户名");
				tv_setting_login.setText("登陆");
				
				loginFlag = false;
				MainActivity.ID=0;
				mapr.clearCache();
				tb_setting_dajiao.setChecked(false);
			}
			else
			{
				HomeActivityGroup.backAction = MainConstants.SZ_ACTION;
				qStartActivity(UserLoginActivity.class, null,MainConstants.SZ_ACTION);
			}
			break;
		case R.id.rl_setting_service:
			qStartActivity(SettingServiceActivity.class, null);
			break;
		case R.id.rl_setting_aboutus:
			qStartActivity(SettingAboutusActivity.class, null);
			break;
		case R.id.rl_setting_feedback:
			qStartActivity(SettingFeedbackActivity.class, null);
			break;
		case R.id.tb_setting_dajiao:
			SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
			SharedPreferences.Editor editor = sp.edit();
			
//			editor.putString("mobileNumber", "");
//			editor.putString("userEmail", "");
//			editor.putString("userRealName", "");
			
			
			if(tb_setting_dajiao.isChecked())
			{
				editor.putInt("notAlert", 1);
			}
			else
			{
				editor.putInt("notAlert", 0);
			}
			editor.commit();
			break;
		case R.id.bt_setting_quit:
			startServices();
//			Intent intent=new Intent(Intent.ACTION_MAIN);
//			intent.addCategory(Intent.CATEGORY_HOME);
//			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			this.startActivity(intent);
//			System.exit(0);
			finish();
			break;
		}
		
	}
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		SharedPreferences sp = getSharedPreferences(
				"userinfo", MODE_PRIVATE);
		String username=sp.getString("userRealName", "");
		if(username.length()>0)
		{
			loginFlag = true;
			tv_setting_username.setText(username);
			tv_setting_login.setText("注销");
		}
		else
		{
			loginFlag = false;
			tv_setting_username.setText("用户名");
			tv_setting_login.setText("登陆");
		}
			
		
		super.onRestart();
	}
	
	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}
	
	private void startServices(){
		Intent i = new Intent(this, PushServices.class);
		startService(i);
	}

}
